package model;

import utils.Pair;

import java.util.HashMap;
import utils.Configuration;

public class ConfigurationModel {
	
	private boolean changed;
	private HashMap<String, String> properties;
	private Pair<String, String> selectedKey;
	
	public ConfigurationModel() {
		selectedKey = null;
		properties = new HashMap<String,String>();
		Configuration conf = new Configuration();
		properties = conf.getAllProperties();
			
		changed = false;
	}
	
	public void setProperties(HashMap<String, String> props) {
		this.properties = props;
	}
	public HashMap<String, String> getProperties() {
		return properties;
	}
	public void setChanged(boolean changed) {
		this.changed = changed;
	}
	public boolean isChanged() {
		return changed;
	}

	public void changeKeyData(String key, String value) {
		selectedKey = new Pair<String, String>(key, value);
		properties.put(key, value);
		changed = true;
		
	}
	
	public void save() {
		
		Configuration conf = new Configuration();
		//Guardamos los cambios en el fichero
		for (String key : properties.keySet()) {
			
			conf.setProperty(key, properties.get(key));
		}
		
		conf.saveProperties();
	}
	
	public void changeKeyDataByDialog(String path) {
		if (selectedKey != null) {
			String key = selectedKey.getFirst();
			selectedKey.setSecond(path);
						
			properties.put(key, path);
			changed = true;
		}
	}
	
	public Pair<String, String> getSelectedKey() {
		return selectedKey;
	}

	public void setSelectedKey(String key, String value) {
		this.selectedKey = new Pair<String,String>(key,value);
		 
	}

}
